package com.ljx.itbbs.server.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ljx.itbbs.server.user.dao.RoleMenuRelationDao;
import com.ljx.itbbs.server.user.domain.entity.RoleMenuRelationEntity;
import com.ljx.itbbs.server.user.domain.vo.req.RoleMenuSaveReq;
import com.ljx.itbbs.server.user.domain.vo.resp.MenuResp;
import com.ljx.itbbs.server.user.service.RoleMenuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-04-02  21:47
 */
@Service
public class RoleMenuRelationServiceImpl implements RoleMenuRelationService {

    @Autowired
    private RoleMenuRelationDao roleMenuRelationDao;

    @Override
    public List<MenuResp> getRoleMenuList(Long roleId) {
        return roleMenuRelationDao.getRoleMenuList(roleId);
    }

    @Override
    public void saveRoleMenu(RoleMenuSaveReq req) {
        // 删除之前存在的菜单
        roleMenuRelationDao.deleteByRoleId(req.getRoleId());
        Long roleId = req.getRoleId();
        List<RoleMenuRelationEntity> roleMenuRelationList = req.getMenuIds().stream().map(item -> {
            RoleMenuRelationEntity roleMenuRelationEntity = new RoleMenuRelationEntity();
            roleMenuRelationEntity.setMenuId(item);
            roleMenuRelationEntity.setRoleId(roleId);
            return roleMenuRelationEntity;
        }).collect(Collectors.toList());
        roleMenuRelationDao.saveBatch(roleMenuRelationList);
    }
}
